package com.source.adnroid.comm.ui.fragment;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.aspsine.swipetoloadlayout.OnLoadMoreListener;
import com.aspsine.swipetoloadlayout.OnRefreshListener;
import com.source.adnroid.comm.ui.activity.UserDetailsActivity;
import com.source.adnroid.comm.ui.adapter.OnItemClickListener;

import java.util.ArrayList;


/**
 * Created by zzw on 2018/4/2.
 */

public class ChatFragment extends ChatBaseFragment implements OnLoadMoreListener, OnRefreshListener, OnItemClickListener {
    private String TAG = "ChatFragment";
    MsgHandler msgHandler = new MsgHandler();

    @Override
    public void initHistoryData() {
        Log.i(TAG, "add");
        list = new ArrayList<String>();
        for (int i = 0; i < 20; i++) {
            list.add("原始数据");
        }
    }


    @Override
    public void updateData() {
        list.add("new msg");
        msgHandler.sendEmptyMessage(0);
    }

    @Override
    public void initListener() {
        //为swipeToLoadLayout设置上拉加载更多监听者
        swipeToLoadLayout.setOnRefreshListener(this);
        swipeToLoadLayout.setOnLoadMoreListener(this);
        chatMessageAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onKeyListener() {
        updateData();
    }

    @Override
    public void onLoadMore() {
        Log.i(TAG, "onLoadMore");
/*        for (int i = 0; i < 20; i++) {
            list.add("加载更多获得的数据");
        }
        chatMessageAdapter.notifyDataSetChanged();*/
        //设置上拉加载更多结束
        swipeToLoadLayout.setLoadingMore(false);
    }

    /**
     * 此处用来查看历史数据
     */
    @Override
    public void onRefresh() {
        Log.i(TAG, "onRefresh");
        for (int i = 0; i < 20; i++) {
            list.add(0, "刷新获得的数据");
        }
        chatMessageAdapter.notifyDataSetChanged();
        //设置下拉刷新结束
        swipeToLoadLayout.setRefreshing(false);
    }

    @Override
    public void onClick(int position) {
        Intent intent = new Intent();
        intent.setClass(getActivity(), UserDetailsActivity.class);
        startActivity(intent);
    }

    @Override
    public void onLongClick(int position) {

    }

    class MsgHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            chatMessageAdapter.notifyDataSetChanged();
            if (isShowLastPosition()) {
                recyclerView.scrollToPosition(chatMessageAdapter.getItemCount() - 1);
            } else {
                Log.d(TAG, "一条未读新消息");
                msgNotRead.setText("有未读消息点击跳转");
            }

        }
    }

    public boolean isShowLastPosition() {
        Log.d(TAG, "lastPosition=>" + linearLayoutManager.findLastVisibleItemPosition() + "ItemCount" + chatMessageAdapter.getItemCount());
        int lastPosition = linearLayoutManager.findLastVisibleItemPosition();
        if (lastPosition == chatMessageAdapter.getItemCount() - 2) {
            return true;
        }
        return false;
    }
}
